import settings from "../utils/settings";
import renderBeaconBeam2 from "../../BeaconBeam";
import { location } from "../utils/getLocation";
import { Angle, MessageID, RemoveEmoji, ScoreboardLine, getEntityHP, getItemSBID, isModLoaded } from "../utils/utils";


export function garden(){}

//显示花园里的虫子
let pests = [];
register("step", () => {
    if(!settings.ShowPests || !location.inSkyblock || location.island != "Garden" || !location.location.includes("ൠ")) return;
    pests = World.getAllEntities().filter(e => e && getEntityHP(e).maxhp === 600 || getEntityHP(e).maxhp === 1200);
}).setDelay(1)
register("renderWorld", () => {
    if(!settings.ShowPests || !location.inSkyblock || location.island != "Garden" || !location.location.includes("ൠ")) return;
    pests.forEach(e => {
        renderBeaconBeam2(e.getX()-0.5,e.getY(),e.getZ()-0.5,1,1,0,1,true);
    })
})

//花园去ding声
let GardenUndingableTimer = 0;
register("hitBlock", () => {
    if(!settings.GardenUndingable || !location.inSkyblock || location.island != "Garden") return;
    GardenUndingableTimer = Date.now();
})
register("soundPlay", (pos,name,vol,pit,cate,event) => {
    if(!settings.GardenUndingable || !location.inSkyblock || location.island != "Garden") return;
    if(GardenUndingableTimer + 500 < Date.now()) return;
    if(name == "random.orb") cancel(event);
})

//左键锁定
const AttackKeyBind = new KeyBind(Client.getMinecraft().field_71474_y.field_74312_F);
let KeyLocked = false;
let PressedTime = 0;
AttackKeyBind.registerKeyDown(() => { PressedTime++ })
AttackKeyBind.registerKeyRelease(() => {
    if(settings.GardenLeftLock == 0 || !location.inSkyblock || location.island != "Garden") return;
    if(KeyLocked) KeyLocked = false;
    else if(PressedTime >= settings.GardenLeftLock * 20) {
        AttackKeyBind.setState(true);
        KeyLocked = true;
        Client.showTitle("","§a左键锁定 已激活",0,30,10);
        World.playSound("random.orb",1,1);
    }
    PressedTime = 0;
})

//告示牌转视角
const angleReg = /(-?\d+[\.]?\d*|)[ ]?\/[ ]?(-?\d+[\.]?\d*)/;
const speedReg = /((速度|speed)[:]?|✦)[ ]*([0-9]{1,3})/i;
let speedNeedToSet = 0;

register("playerInteract", (action,pos,event) => {
    if(!settings.AutoRotate || !location.inSkyblock || location.island != "Garden") return;
    if(action.toString() != "RIGHT_CLICK_BLOCK") return;
    const item = Player.getHeldItem();
    if(!item || !(item.getRegistryName().endsWith("_axe") || item.getRegistryName().endsWith("_hoe"))) return;
    const blocks = World.getAllTileEntitiesOfType(net.minecraft.tileentity.TileEntitySign)
        .filter(e => e.getX() == pos.x && e.getY() == pos.y && e.getZ() == pos.z)
    if(blocks.length != 1) return;
    const signText = blocks[0].tileEntity.field_145915_a;  //TileEntitySign.signText
    for (let e of signText) {
        let text = e.func_150260_c();
        let angle = angleReg.exec(text);
        if(angle) {
            Angle.setAngle(angle[1],angle[2]);
            new Message("§6§l[BZA] §a已根据告示牌所示帮您转动视角.\n§a当前偏航角§7:§e " + Player.getYaw() + " §a俯仰角§7:§e " + Player.getPitch())
                .setChatLineId(MessageID.GardenRotationAngle).chat();
            cancel(event);
            continue;
        }
        let speedRegExpExecArray = speedReg.exec(text);
        if(speedRegExpExecArray) {
            let speed = parseInt(speedRegExpExecArray[3]);
            let boots = Player.armor.getBoots();
            if(!boots || getItemSBID(boots) != "RANCHERS_BOOTS") continue;
            let bootsSpeed = boots.getLore().filter(e => e.removeFormatting().startsWith("Current Speed Cap: ")).map(e => parseInt(e.removeFormatting().split(":")[1].trim())).shift();
            if(bootsSpeed && bootsSpeed == speed) continue;
            speedNeedToSet = speed;
            setTimeout(() => {
                ChatLib.command("setmaxspeed",false);
            }, 50 + Math.floor(Math.random() * 150));
            continue;
        }
    }
})

register("guiOpened", (event) => {
    if(!settings.AutoRotate || !location.inSkyblock || location.island != "Garden" || speedNeedToSet == 0) return;
    setTimeout(() => {
        let gui = Client.getMinecraft().field_71462_r;
        if(!gui) return;
        let sign = isModLoaded("skytils") && gui.toString().includes("GuiEditSign") ? gui.getTileSign() : gui.field_146848_f;
        if(!sign) return;
        let text = sign.field_145915_a;
        if(text[3].func_150260_c() != "speed cap!") return;
        text[0] = new TextComponent(speedNeedToSet).chatComponentText;
        speedNeedToSet = 0;
        setTimeout(() => {
            Player.getPlayer().func_175159_q();
        }, 150 + Math.floor(Math.random() * 150));
    }, 50);
})